-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.


function onInit()
	Interface.onWindowOpened = onWindowOpened;
	Interface.onWindowClosed = onWindowClosed;
end

function onWindowOpened(window)
	local sourcename = "";
	if window.getDatabaseNode() then
		sourcename = window.getDatabaseNode().getNodeName();
	end

	if CampaignRegistry.windowpositions then
		if CampaignRegistry.windowpositions[window.getClass()] then
			if CampaignRegistry.windowpositions[window.getClass()][sourcename] then
				local pos = CampaignRegistry.windowpositions[window.getClass()][sourcename];
				
				window.setPosition(pos.x, pos.y);
				window.setSize(pos.w, pos.h);
			end
		end
	end
end

function onWindowClosed(window)
	if not CampaignRegistry.windowpositions then
		CampaignRegistry.windowpositions = {};
	end
	
	if not CampaignRegistry.windowpositions[window.getClass()] then
		CampaignRegistry.windowpositions[window.getClass()] = {};
	end
	
	-- Get window data source node name
	local sourcename = "";
	if window.getDatabaseNode() then
		sourcename = window.getDatabaseNode().getNodeName();
	end
	
	-- Get window positioning data
	local x, y = window.getPosition();
	local w, h = window.getSize();
	
	-- Store positioning data
	local pos = {};
	pos.x = x;
	pos.y = y;
	pos.w = w;
	pos.h = h;
	
	CampaignRegistry.windowpositions[window.getClass()][sourcename] = pos;
end
